#include "stdafx.h"
#include "CVHeader.h"
#include "ImageHandler.h"
#include "GlobalFunc.h"
/*#include <vld.h>*/
#include <iostream>

using namespace GlobalFunc;
using namespace std;
using namespace cv;
using namespace HLCAO;

int main(int argc, _TCHAR* argv[])
{
// 	GlobalFunc::CapWithKinect();


	//build templates------------------------
	IplImage* pMaskImg = cvLoadImage("image\\template\\imgTemp_m.png", 0);

	ImageHandler imgTemHandler;
	imgTemHandler.loadImage("image\\template\\imgTemp_t.png");
	imgTemHandler.constructGradientImage();
	imgTemHandler.initGridInfo();
	imgTemHandler.generateImgTemplateByteMat();
	imgTemHandler.setMaskImgWithPointer(pMaskImg);
	imgTemHandler.generateCntKeyPoints();


	
	cvNamedWindow("depImgView", CV_WINDOW_AUTOSIZE);
	IplImage* pTempImage = cvLoadImage("image\\template\\depTemp_dt.png", -1);
	IplImage* pNewImage =  GlobalFunc::convertTo32FImg(pTempImage);
	ImageHandler depthHandler;
	depthHandler.setMaskImgWithPointer(pMaskImg);
	depthHandler.setImageWithPointer(pNewImage);
	depthHandler.constructGradientImage();
	depthHandler.initGridInfo();
	depthHandler.generateDepTemplateByteMat();
	depthHandler.generateDepKeyPoints();
	//convert depth map to grey image to view it
	IplImage* depthImage = GlobalFunc::scaleToGreyScale(pTempImage);
// 
// 	//test with template matching
	GlobalFunc::TestWithTemplateMatching(imgTemHandler, depthHandler);


	//test with target 
	ImageHandler tarImageHander;
	tarImageHander.loadImage("image\\template\\banana.png");
	tarImageHander.constructGradientImage();
	tarImageHander.initGridInfo();
	tarImageHander.generateImgTargetByteMat();

	IplImage* pTargetDep = cvLoadImage("image\\template\\banana_td.png", -1);
	IplImage* _pTargetDep = GlobalFunc::convertTo32FImg(pTargetDep);
	ImageHandler targetDephandler;
	targetDephandler.setImageWithPointer(_pTargetDep);
	targetDephandler.constructGradientImage();
	targetDephandler.initGridInfo();
	targetDephandler.generateDepTargetByteMat();


	hlTimeCounter timer;
	timer.Start();
	MatchResult result = TemplateMatching(imgTemHandler, tarImageHander,depthHandler, targetDephandler);
	timer.End();
	std::cout<<"tempalte matching time: " << timer.getElapseTime()<<std::endl;

// 	//label the key points---------------
	std::vector<Saliency>& saliVec = depthHandler.getDepKeyPoints();
	std::vector<Saliency>& imgSali = imgTemHandler.getCntKeyPoints();
	for (size_t i=0; i<saliVec.size(); ++i)
	{
		Saliency& saliency = saliVec[i];
		if (saliency.hisValue > 0)
		{
			cvRectangle( depthImage, cvPoint(saliency.iGPos*GRID_SIZE, saliency.jGPos*GRID_SIZE),
				cvPoint(saliency.iGPos*GRID_SIZE + GRID_SIZE, saliency.jGPos*GRID_SIZE+GRID_SIZE),
				cvScalar(255, 255, 255));
		}
		Saliency& _salient = imgSali[i];
		int val = 255;
		ImageHandler::setGridValue(imgTemHandler.getImage(), _salient.jGPos, _salient.iGPos, &val);
		cvRectangle(				imgTemHandler.getMaskImg(), 
									cvPoint(_salient.iGPos*GRID_SIZE, _salient.jGPos*GRID_SIZE),
									cvPoint(_salient.iGPos*GRID_SIZE+GRID_SIZE, _salient.jGPos*GRID_SIZE+GRID_SIZE),
									cvScalar(200, 200, 200));

	}


	cvNamedWindow("maskView", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("targetImageView", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("templateView", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("depth", CV_WINDOW_AUTOSIZE);

	cvShowImage("maskView", imgTemHandler.getMaskImg());
	cvShowImage("templateView", imgTemHandler.getImage());
	cvShowImage("targetImageView", tarImageHander.getImage());
	cvShowImage("depth", depthImage);

	cvSaveImage("labelImg.png", imgTemHandler.getImage());
	cvSaveImage("labelMask.png", imgTemHandler.getMaskImg());
	cvSaveImage("depthImg.png", depthImage);
	cvWaitKey(0);
	return 0;
}

